package com.bianguo.android.bprinter.runnable;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.graphics.Bitmap;

import com.bianguo.android.baselibrary.utils.Contacts;
import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.bprinter.utils.BitmapUtil;
import com.bianguo.android.bprinter.utils.Print;
import com.bianguo.android.bprinter.utils.PrintUtil;
import com.bianguo.android.bprinter.utils.PrinterCmdUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

public class BluetoothRunnable implements Runnable {

    // 复位打印机
    public static final byte[] RESET = {0x1b, 0x40};
    // 左对齐
    public static final byte[] ALIGN_LEFT = {0x1b, 0x61, 0x00};
    // 中间对齐
    public static final byte[] ALIGN_CENTER = {0x1b, 0x61, 0x01};
    // 右对齐
    public static final byte[] ALIGN_RIGHT = {0x1b, 0x61, 0x02};
    // 选择加粗模式
    public static final byte[] BOLD = {0x1b, 0x45, 0x01};
     // 取消加粗模式
    public static final byte[] BOLD_CANCEL = {0x1b, 0x45, 0x00};
     // 宽高加倍
    public static final byte[] DOUBLE_HEIGHT_WIDTH = {0x1d, 0x21, 0x11};
     // 宽加倍
    public static final byte[] DOUBLE_WIDTH = {0x1d, 0x21, 0x10};
     // 高加倍
    public static final byte[] DOUBLE_HEIGHT = {0x1d, 0x21, 0x01};
     // 字体不放大
    public static final byte[] NORMAL = {0x1d, 0x21, 0x00};
     // 设置默认行间距
    public static final byte[] LINE_SPACING_DEFAULT = {0x1b, 0x32};

    private BluetoothSocket mSocket;
    private BluetoothAdapter adapter;

    public BluetoothRunnable(BluetoothAdapter adapter) {
        this.adapter = adapter;
    }

    public void upSocket(BluetoothDevice device) {
        if (this.mSocket != null) {
            cancel();
        }
        BluetoothSocket tempSocket = null;
        try {
            tempSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(Contacts.SerialPortServiceClass_UUID));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.mSocket = tempSocket;
    }

    @Override
    public void run() {
        adapter.cancelDiscovery();
        try {
            mSocket.connect();
        } catch (IOException e) {
            e.printStackTrace();
            MLog.mLog("connect error : " + e.getMessage());
        }
        boolean isConnected = mSocket.isConnected();
        MLog.mLog("connect success isSuccess = " + isConnected);
        BluetoothDevice remoteDevice = mSocket.getRemoteDevice();
        MLog.mLog(remoteDevice.getName() + ", " + remoteDevice.getAddress() + ", " + remoteDevice.getBondState() + ", " + remoteDevice.getType());
        if (isConnected) {
        }
    }

    public synchronized void sendData(String data) {
        OutputStream os = null;
        if (mSocket != null && mSocket.isConnected()) {
            try {
                os = mSocket.getOutputStream();
                MLog.mLog("--------------- 开始打印 --------------\ndata = " + data);
                new Print.Builder()
                        .setWriter(os)
                        .init()
                        .setAlignPosition(0)
                        .printTxt(data)
                        .build();
            } catch (IOException e) {
                e.printStackTrace();
                MLog.mLog("sendData error : " + e.getMessage());
            } finally {
            }
        } else {
            MLog.mLog("设备未连接，请重新连接设备");
        }
    }

    public synchronized void printImg(Bitmap bitmap) {
        OutputStream os = null;
        if (mSocket != null && mSocket.isConnected()) {
            try {
                os = mSocket.getOutputStream();
                MLog.mLog("--------------- 开始打印 --------------");
                //发送打印图片前导指令
                byte[] start = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B,
                        0x40, 0x1B, 0x33, 0x00 };
                os.write(start);
                os.flush();
                byte[] readBitMapBytes = BitmapUtil.draw2PxPoint(bitmap);
                os.write(readBitMapBytes);
                os.flush();
                //发送结束指令
                byte[] end = { 0x1d, 0x4c, 0x1f, 0x00 };
                os.write(end);
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
                MLog.mLog("sendData error : " + e.getMessage());
            } finally {
            }
        } else {
            MLog.mLog("设备未连接，请重新连接设备");
        }
    }

    public void cancel() {
        MLog.mLog("close bluetooth socket");
        try {
            mSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
            MLog.mLog("close() of connect socket failed", e.getMessage());
        }
    }

    public BluetoothSocket getSocket() {
        return mSocket;
    }
}
